using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using FAQComponents;
using FAQComponents.Configuration;
using FAQComponents.Objects;
using FAQComponents.API;
using Adge.UI.WebControls;

namespace Adge.Web.Faq.Admin
{
	/// <summary>
	/// Summary description for CreateFAQPart.
	/// </summary>
	public partial class CreateFAQPart : ClientSidePage
	{

		private int FAQID;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			FAQID = InvariantString.ToInt32(Request.QueryString["ID"]);

			MonitorChanges(answer);
			BypassModifiedMethod(btnCreateFAQ);

			if (!Page.IsPostBack)
			{
				FAQ f = FAQAPI.GetFAQ(FAQID);
				PermissionAPI.EnsureValidContributorForCategory(f.CategoryID);

				question.Text = f.Question;
				
				category.DataSource = CategoryAPI.GetAllCategories();
				category.DataBind();

				if (category.Items.FindByValue(f.CategoryID.ToString()) != null)
					category.Items.FindByValue(f.CategoryID.ToString()).Selected = true;

				currentAnswer.Text = f.AnswerAsHtmlString;
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void btnCancel_Click(object sender, System.EventArgs e)
		{
			Response.Redirect(ResolveUrl(string.Format(FAQConfiguration.GetConfig().ShowFAQUrl, FAQID)));
		}

		protected void btnCreateFAQ_Click(object sender, System.EventArgs e)
		{
			User u = FAQContext.GetContext().LoggedOnUser;

			FAQPart fp = new FAQPart();
			fp.FAQID = FAQID;
			fp.CreatedByUserID = u.UserID;
			fp.Answer = answer.Text;
			fp.Approved = PermissionAPI.IsAdminFAQEditorOrTrustedContributor(FAQAPI.GetFAQ(FAQID).CategoryID);

			FAQAPI.AddFAQPart(fp);

			Response.Redirect(ResolveUrl(string.Format(FAQConfiguration.GetConfig().ShowFAQUrl, FAQID)));
		}
	}
}
